Skip to content

Add: Initial setup for mypy checking.#445

Merged
oscarlevin merged 7 commits intoPreTeXtBook:mainfrom
bjones1:mypy
Jun 20, 2023
Merged

Add: Initial setup for mypy checking.#445
oscarlevin merged 7 commits intoPreTeXtBook:mainfrom
bjones1:mypy

Conversation

@bjones1
Copy link
Copy Markdown
Collaborator

@bjones1 bjones1 commented Jun 1, 2023

This enables mypy type checking (a goal of #431), plus fixes types in utils.py. I can extend this to fix all type errors mypy report -- should I keep going, or would you like to review these changes in more detail?

To run: mypy --install-types --non-interactive.

The current list of type errors:

pretext\templates\__init__.py:17: error: Incompatible return value type (got "AbstractContextManager[Path]", expected "Path")  [return-value]
pretext\templates\__init__.py:19: error: Incompatible return value type (got "AbstractContextManager[Path]", expected "Path")  [return-value]
pretext\codechat.py:86: note: Revealed type is "lxml.etree._ElementTree"
pretext\codechat.py:90: error: "_ElementTree" has no attribute "iterfind"  [attr-defined]
pretext\generate.py:33: error: Incompatible types in assignment (expression has type "Dict[Union[str, Any], List[str]]", target has type "Optional[List[str]]")  [assignment]
pretext\generate.py:33: error: Item "None" of "Optional[List[str]]" has no attribute "__iter__" (not iterable)  [union-attr]
pretext\generate.py:41: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:51: error: Item "None" of "Optional[List[str]]" has no attribute "__iter__" (not iterable)  [union-attr]
pretext\generate.py:96: error: Incompatible types in assignment (expression has type "Dict[str, List[str]]", target has type "List[str]")  [assignment]
pretext\generate.py:104: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:157: error: Incompatible types in assignment (expression has type "Dict[str, List[str]]", target has type "List[str]")  [assignment]
pretext\generate.py:164: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:203: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:239: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:273: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:309: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:342: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:343: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:375: error: Module has no attribute "play_button"  [attr-defined]
pretext\generate.py:394: error: Argument 1 to "len" has incompatible type "Union[bool, float, str, bytes, List[Union[_Element, Union[_ElementUnicodeResult, _PyElementUnicodeResult, _ElementStringResult], Union[str, bytes], Tuple[Union[str, bytes, None], Union[str, bytes, None]]]]]"; expected "Sized"  [arg-type]
pretext\generate.py:400: error: Module has no attribute "datafiles_to_xml"  [attr-defined]
pretext\build.py:44: error: Argument 1 to "map_path_to_xml_id" has incompatible type "Path"; expected "str"  [arg-type]
pretext\build.py:44: error: Argument 2 to "map_path_to_xml_id" has incompatible type "Optional[Path]"; expected "Path"  [arg-type]
pretext\build.py:154: error: Argument 1 to "working_directory" has incompatible type "str"; expected "Path"  [arg-type]
pretext\build.py:182: error: Argument 1 to "working_directory" has incompatible type "str"; expected "Path"  [arg-type]
pretext\build.py:213: error: Argument 1 to "working_directory" has incompatible type "str"; expected "Path"  [arg-type]
pretext\build.py:243: error: Module has no attribute "webwork_sets"  [attr-defined]
pretext\config\xml_overlay.py:38: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
pretext\config\xml_overlay.py:60: error: Argument 1 to "append" of "list" has incompatible type "Tuple[str, Optional[str]]"; expected "Tuple[str, str]"  [arg-type]
pretext\config\xml_overlay.py:71: error: Function "lxml.etree.Element" is not valid as a type  [valid-type]
pretext\config\xml_overlay.py:71: note: Perhaps you need "Callable[...]" or a callback protocol?
pretext\config\xml_overlay.py:83: error: Function "lxml.etree.Element" is not valid as a type  [valid-type]
pretext\config\xml_overlay.py:83: note: Perhaps you need "Callable[...]" or a callback protocol?
pretext\config\xml_overlay.py:85: error: Function "lxml.etree.Element" is not valid as a type  [valid-type]
pretext\config\xml_overlay.py:85: note: Perhaps you need "Callable[...]" or a callback protocol?
pretext\config\xml_overlay.py:94: error: ET.Element? has no attribute "__iter__" (not iterable)  [attr-defined]
pretext\config\xml_overlay.py:98: error: ET.Element? has no attribute "append"  [attr-defined]
pretext\config\xml_overlay.py:113: error: ET.Element? has no attribute "get"  [attr-defined]
pretext\config\xml_overlay.py:121: error: ET.Element? has no attribute "set"  [attr-defined]
pretext\config\xml_overlay.py:123: error: ET.Element? has no attribute "text"  [attr-defined]
pretext\config\xml_overlay.py:126: error: ET.Element? has no attribute "text"  [attr-defined]
pretext\config\xml_overlay.py:135: error: ET.Element? has no attribute "text"  [attr-defined]
pretext\project.py:28: error: Function "lxml.etree.Element" is not valid as a type  [valid-type]
pretext\project.py:28: note: Perhaps you need "Callable[...]" or a callback protocol?
pretext\project.py:35: error: Element? has no attribute "get"  [attr-defined]
pretext\project.py:38: error: Element? has no attribute "get"  [attr-defined]
pretext\project.py:45: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:48: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:59: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:91: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:95: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:98: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:101: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:114: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:116: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:145: error: Function "lxml.etree.Element" is not valid as a type  [valid-type]
pretext\project.py:145: note: Perhaps you need "Callable[...]" or a callback protocol?
pretext\project.py:168: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:170: error: Element? has no attribute "find"  [attr-defined]
pretext\project.py:174: error: Incompatible return value type (got "None", expected "Target")  [return-value]
pretext\cli.py:362: error: Incompatible types in assignment (expression has type "Tuple[str, str, Tuple[str, str]]", variable has type "Tuple[str, str]")  [assignment]
pretext\cli.py:364: error: Unpacking a string is disallowed  [misc]
pretext\cli.py:365: error: Cannot determine type of "path"  [has-type]
pretext\cli.py:365: error: Cannot determine type of "value"  [has-type]
pretext\cli.py:449: error: Unpacking a string is disallowed  [misc]
pretext\cli.py:450: error: Cannot determine type of "path"  [has-type]
pretext\cli.py:450: error: Cannot determine type of "value"  [has-type]
pretext\cli.py:458: error: Incompatible types in assignment (expression has type "Target", variable has type "Optional[str]")  [assignment]
pretext\cli.py:461: error: Item "str" of "Optional[str]" has no attribute "name"  [union-attr]
pretext\cli.py:461: error: Item "None" of "Optional[str]" has no attribute "name"  [union-attr]
pretext\cli.py:464: error: Argument 1 to "show_target_hints" has incompatible type "Optional[str]"; expected "str"  [arg-type]
pretext\cli.py:469: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:471: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:474: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:477: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:481: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:483: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:486: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:488: error: "str" has no attribute "name"  [attr-defined]
pretext\cli.py:583: error: "str" has no attribute "relative_to"  [attr-defined]
Found 73 errors in 7 files (checked 11 source files)

@oscarlevin
Copy link
Copy Markdown
Member

This looks like a great start. As we discussed in drop-in, I'll wait for you to add more coverage before merging.

@bjones1 bjones1 marked this pull request as ready for review June 20, 2023 20:03
@oscarlevin oscarlevin merged commit 54628ca into PreTeXtBook:main Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants